package ltd.hxya.novel.common.utils;
import io.jsonwebtoken.*;
import ltd.hxya.novel.common.bean.JwtBean;

import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.Objects;


public class JwtUtils {


    public static String getToken(Object value) {

        return subjectToken(value).compact();
    }

    public static JwtBuilder subjectToken(Object value){
        return jwtBuilder().setSubject(JsonUtils.objectToString(value));
    }

    public static JwtBuilder jwtBuilder(){
        JwtBean jwtBean = new JwtBean();
        return Jwts.builder().signWith(SignatureAlgorithm.HS256,jwtBean.getSecret())
                .setExpiration(new Date(System.currentTimeMillis() + jwtBean.getExpireTime()));
    }

    public static String checkToken(String token){
        return getClaims(token).getSubject();
    }

    public static Claims getClaims(String token){

        return getJwtParser().parseClaimsJws(token).getBody();
    }

    public static JwtParser getJwtParser(){

        JwtBean jwtBean = new JwtBean();
        return Jwts.parser().setSigningKey(jwtBean.getSecret());
    }

}
